package com.leecode.exercise;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: shubin_yao
 * @Date: 2022/06/25/21:12
 * @Description:
 * 两棵二叉搜索树中的所有元素，升序排列
 * 到目前位置，这是唯一自己作对的题目
 *
 * 输入：root1 = [2,1,4], root2 = [1,0,3]
 * 输出：[0,1,1,2,3,4]
 */
class LeeCose_1035_TreeToSortList {

    public static void main(String[] args) {
        TreeNode root1 = new TreeNode();
        TreeNode root2 = new TreeNode();
        /*TreeNode root1 = new TreeNode(1);
        TreeNode node5 = new TreeNode(2);
        TreeNode node6 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node2 = new TreeNode(5);
        TreeNode node3 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        root1.left = node2;
        root1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;*/

     /*   TreeNode root2 = new TreeNode(1);
        TreeNode node25 = new TreeNode(2);
        TreeNode node26 = new TreeNode(3);
        TreeNode node24 = new TreeNode(4);
        TreeNode node22 = new TreeNode(5);
        TreeNode node23 = new TreeNode(6);
        TreeNode node27 = new TreeNode(7);
        root2.left = node22;
        root2.right = node23;
        node2.left = node24;
        node2.right = node25;
        node3.left = node26;
        node3.right = node27;*/

        Solution4 solution4 = new Solution4();
        solution4.getAllElements(root1, root2);

    }
}


class Solution4 {
    public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {

        List<Integer> list = new ArrayList<>();
        List<Integer> list1 = dfs(root1, list);
        List<Integer> list2 = dfs(root2, list1);
        Collections.sort(list2);
        System.out.println(list2);
        return list2;
    }

    public List<Integer> dfs(TreeNode root, List<Integer> list) {
        if(root == null){
            return list;
        }
        Integer v1 = root.val;
        if (root.left != null) {
            dfs(root.left, list);
        }
        if (root.right != null) {
            dfs(root.right, list);
        }
        list.add(v1);
        return list;
    }
}
